package com.hnust.root.service.impl.forward.wx;

import com.hnust.root.common.model.MapData;
import com.hnust.root.dao.entity.domain.forward.WxUserInfo;
import com.hnust.root.service.forword.wx.WxUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MapDataService {
    @Autowired
    private WxUserInfoService userInfoService;

    //通过学校名称返回地图数据
    public List<MapData> getMapDataByUniversity(String university) {
        List<WxUserInfo> userInfoList = userInfoService.getUserInfoByUniversity(university);
        Map<String, Integer> map = new HashMap<>();

        List<MapData> mapDataList = new ArrayList<>();
        userInfoList.forEach(userinfo -> {
            String province = userinfo.getProvince();
            map.put(province, map.getOrDefault(province, 0) + 1);
        });

        map.forEach((key,value) -> {
            MapData mapData = new MapData();
            mapData.setName(key);
            mapData.setValue(value);
            mapDataList.add(mapData);
        });
        return mapDataList;
    }

    //通过学校获取用户数量及地区数量
    public Map<String,Integer> getUserAndCityCount(String university) {
        List<WxUserInfo> userInfoList = userInfoService.getUserInfoByUniversity(university);
        Set<String> citySet = new HashSet<>();
        userInfoList.forEach(userInfo -> {
            citySet.add(userInfo.getCity());
        });

        Map<String, Integer> map = new HashMap<>(2);
        map.put("userCount", userInfoList.size());
        map.put("locationCount", citySet.size());
        return map;
    }

}
